<?php
class BadWordForm extends Form{
	function BadWordForm(){
		Form::Form('BadWordForm');	
		$this->link_css('style/manage_item.css');				
	}	
	
	function on_submit(){	
		if(Url::get("del_all")=="Xóa Từ Xấu")
		{
			$ids = (isset($_POST['chk_id']))?$_POST['chk_id']:array(); 
			$bw_ids='';							
			if(count($ids)>0){		
				for($i=0;$i<count($ids);$i++){
					$bw_ids.=($bw_ids?',':'').$ids[$i];
				}		
				DB::query('DELETE FROM bad_words WHERE id IN('.$bw_ids.')');		
				Url::redirect_url(Url::build_all(array('chk_id','del_all','cmd','id','contents')));		
			}	
		}
	}
	
	function draw(){
		$this->beginForm();	
		global $display;		
		$str_badword = stripslashes(Url::get("bad_word"));
		$show_bw = array();
		if(Url::get("submit")=="Kiểm tra" && $str_badword){
			$show_bw = array();
			$show_bw = AZLib::checkBadWord($str_badword,true,false,true);
			$display->add('show_bw',$show_bw);
		}
		$display->add('bad_word',$str_badword);
		
		
		$search_value = ' contents <>"" ';
		
		if(Url::get('contents')){
			$search_value .= ' AND contents like "%'.Url::get('contents').'%"  OR reason like "%'.Url::get('contents').'%" ';
		}
		
		$item_per_page = 100;	
		$sql_count='SELECT COUNT(*) AS total_item FROM bad_words WHERE '.$search_value;
		$total=DB::fetch($sql_count,'total_item',0);
			
		$items=array();		
		if($total){
			$limit='';
			$paging = AZPagging::pagingSE($limit,$total,$item_per_page,10,'page_no',true,'Tin','Trang');			
			$sql = 'SELECT * FROM bad_words WHERE '.$search_value.' ORDER BY exact DESC, id DESC '.$limit;					
			$result = DB::query($sql);
			
			if($result){
				while ($row=mysql_fetch_assoc($result)){					
					$row['del_link'] = Url::build_all(array('chk_id','del_all','cmd','id','contents'),'cmd=delete&id='.$row['id']);
					if($row['is_phone']==1){
						$row['contents'] = BadWord::filter_badword_show($row['contents']);
					}
					$items[$row['id']]=$row;
				}				
			}
		}else 
		{
			$paging = '';
		}				
				
		$display->add('items',$items);
		$display->add('url_add',WEB_DIR.AZRewrite::formatUrl('?page=manage_badword&cmd=add'));		
		$display->add('contents',Url::get('contents'));		
		$display->add('paging',$paging);		
		$display->output('list');
		$this->endForm();
	}
	
}
?>